/**
 * class name: inav,
 * copyright: maximus zhang,
 * License: MIT-LICENSE,
 * contact: maximus.zhang@gmail.com,
 * required-js: http://hcjs.googlecode.com/svn/trunk/core.js,
 * update: http://hcjs.googlecode.com/svn/trunk/inav.js
 * ▼▲ &#9660; &#9650; > &#9658; < &#9668;
 */
function inav(){
    var A = arguments,
        _ = this;
    _ = ext( _, ext( {
        id : 'menu'+ now(3),
        elem : me.body,
        cssname : 'inav',
        menu : [
            /* each item has the following properties
                split can only have txt==='-|-' property
            */
            {
            id : '',
            cls : '',
            css : '',
            a : { id : '_'+ now(3), href : '', cls : '', css : ''},
            ico : {src:'', alt:''},
            label : {txt:'-|-', css:''},
            key : '',
            menu : [],
            fn : function(){;}
            }
        ]
        }, A[0] ) );

    _.uitype = 'inav';

    _.elem = $(_.elem) || me.body;
}
inav.prototype = {
    hide : function () {
        return false;
    },

    add : function (om) {
        var _ = this;
        if ( om.rtl ) {
            if ( this.root_right ) {
                var el = this.root_right;
            } else {
                var el = this.root_right = elm('div').appendTo(this.$)
                    .setClass('ltr rtl').attr('id', this.id +'_rroot');
            }
        } else {
            if ( this.root_left ) {
                var el = this.root_left;
            } else {
                var el = this.root_left = elm('div').appendTo(this.$)
                    .setClass('ltr').attr('id', this.id +'_lroot');
            }
        }

        var li = elm( 'div' ).setClass( 'item' ).appendTo(el);

        if ( p( om, 'id' ) ) {
            li.id = !$( om.id ) ? om.id : '_item_'+ om.id + now(3);
        } else {
            li.id = '_item__'+ now(2)*Math.random();
        }

        if ( p( om, 'cls' ) ) {
            li.addClass(om.cls);
        }

        if ( p(om, 'css') ) {
            li.style.cssText +=  om.css;
        }

        if ( p(om, 'label', 'txt') && om.label.txt=='-|-' ) {
            li.id = '_splt_'+ li.id;
            li.setClass( 'inavsplt' );

        return this;
        }

        var a = elm('a').appendTo(li).setClass('nor').attr('id', li.id +'_a');
        a.href = p(om, 'href') ? om.href : '#'+ li.id;

        if ( om.fn ) {
            if ( typeof om.fn=='function' ) {
                a.onclick = function () {
                    om.fn.apply(this,[]);
                    return _.hide();
                };
            } else if ( typeof om.fn=='string' ) {
                a.onclick = new Function( om.fn +';return false;' );
            } else {
                a.onclick = function(){return false;};
            }
        } else {
            a.onclick = function(){return false;};
        }

        if ( p(om, 'ico', 'src') ) {
            var ico = elm('img').appendTo(a).attr('src', om.ico.src)
                    .attr('alt', om.ico.alt || '' ).setClass('ico');
        }

        if ( om.label ) {
            var tx = elm('span').appendTo(a);
            if ( p(om, 'label', 'id') && !$(om.label.id) ) {
                tx.id = om.label.id;
            }

            tx.setClass( 'txt'+ (p(om, 'label', 'cls') ? ' '+ om.label.cls : '') );

            if ( p(om, 'label', 'txt') ) {
                tx.innerHTML = om.label.txt;
            }
        }

    return this;
    },

    init : function () {
        if ( !this.$ ) {
            this.$ = elm( 'div' ).appendTo( this.elem );
            this.$.id = this.id = !$(this.id) ? this.id : '_inav'+ this.id;
            p(this, 'cssname') && this.$.setClass(this.cssname);
        }

        for ( var i in this.menu ) {
        this.add(this.menu[i]);
        }

    return this;
    }
};